package com.ruoyi.framework.config;

import com.github.pagehelper.PageInterceptor;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Mybatis Flex 配置
 *
 * @author likeyoudo
 */
@Configuration
public class MybatisFlexConfig implements MyBatisFlexCustomizer {
    private static final Logger logger = LoggerFactory
            .getLogger("mybatis-flex-sql");

    public MybatisFlexConfig() {
        // 开启审计功能
        AuditManager.setAuditEnable(false);

        // 设置 SQL 审计收集器
        AuditManager.setMessageCollector(auditMessage ->
                logger.info("{}, -- {}ms", auditMessage.getFullSql()
                        , auditMessage.getElapsedTime())
        );
    }

    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

    /**
     * 可以对 MyBatis-Flex 进行一系列的初始化配置
     *
     * @param globalConfig
     */
    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        // 设置全局主键策略
        // FlexGlobalConfig.KeyConfig keyConfig = globalConfig.getKeyConfig();
        // keyConfig.setKeyType(KeyType.Auto);
    }
}
